﻿using MediatR;

namespace MediatRApp.Commands
{ 
    public class CreateBookCommand:IRequest
    {
        public string Name { get; set; }
    }
    public class CreateBookCommandEvent : INotification
    {
        public int BookId { get; set; }
        //public string Name { get; set; }

        public CreateBookCommandEvent(int bookId)
        { 
            BookId = bookId;
        }
    }

    public class MQCreateBookCommandEventHander : INotificationHandler<CreateBookCommandEvent>
    { 
        public Task Handle(CreateBookCommandEvent notification, CancellationToken cancellationToken)
        {
            Console.WriteLine("已经发布MQ，给" + notification.BookId);
            return Task.CompletedTask;
        }
    }
    public class EmailCreateBookCommandEventHander : INotificationHandler<CreateBookCommandEvent>
    {
        public Task Handle(CreateBookCommandEvent notification, CancellationToken cancellationToken)
        {
            Console.WriteLine("已经发布Email，给" + notification.BookId);
            return Task.CompletedTask;
        }
    }
     
    public class CreateBookCommandHander : IRequestHandler<CreateBookCommand>
    {
        private readonly IMediator _mediator;
        private readonly DbContext _context;
       
        public CreateBookCommandHander(IMediator mediator, DbContext context)
        {
            _mediator = mediator;
            _context = context;
        }

        public async Task Handle(CreateBookCommand request, CancellationToken cancellationToken)
        {
            var sql = "insert into biz_book(type,name) values(1,@name);select LAST_INSERT_ID();";
            var id = await _context.ExecuteScalar<int>(sql, new { request.Name });


            Console.WriteLine("创建成功");

            await _mediator.Publish(new CreateBookCommandEvent(id)); 

            await _mediator.Send(new CreateSysUserCommand($"张三{id}","123456",18));
        }         
    }



    public class CreateSysUserCommand : IRequest
    {
        public string username { get; set; }

        public string  password { get; set; }

        public int age { get; set; }

        public DateTime createtime { get; set; } = DateTime.Now;

        public DateTime? updatetime { get; set; }= DateTime.Now;

        public CreateSysUserCommand(string name,string pwd,int ag)
        { 
            username=name;
            password=pwd;
            age = ag;
            createtime = DateTime.Now;
            updatetime = DateTime.Now;
        }
    }

    public class CreateSysUserCommandHander : IRequestHandler<CreateSysUserCommand>
    {
        private readonly IMediator _mediator;
        private readonly DbContext _context;

        public CreateSysUserCommandHander(IMediator mediator, DbContext context)
        {
            _mediator = mediator;
            _context = context;
        }
         
        public async Task Handle(CreateSysUserCommand request, CancellationToken cancellationToken)
        {
            var sql = "insert into sysuser(username,password,age,createtime,updatetime) values(@username,@password,@age,@createtime,@updatetime);select LAST_INSERT_ID();";
            var id = await _context.ExecuteScalar<int>(sql, new { request.username, request.password,request.age,request.createtime,request.updatetime }); 
             
            Console.WriteLine("创建成功" + id);
        }
    }

}